package com.yty.demo._12_io;

import java.io.*;

/**
 * @author: yangtaoyao
 * @time: 2020/1/16 12:53
 * @description: 缓冲流
 */
public class BufferedIO {
    public static void main(String[] args) {
        /* 文件字节流
        */
        bufferedInputStreamTest("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\test1.txt");
        bufferedOutputStreamTest("src/com/yty/demo/_12_io/txt/OutputStreamOut.txt","bufferedOutputStreamTest");

        /**
         * 文件字符流
         */
        bufferedReaderTest("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\test1.txt");
        bufferedWriterTest("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\fileWriterOut.txt","fileWriterOut.txt");
        bufferedCopyFile("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\OutputStreamOut.txt", "D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\bufferedCopyFile.txt");

    }

    public static void bufferedInputStreamTest(String path){
        try {
            FileInputStream in=new FileInputStream(path);

            BufferedInputStream bi=new BufferedInputStream(in);

            byte[] b=new  byte[1024];
            int len=0;
            //in.read(byte b[])  //返回读取的数据的长度,如果读取到最后一个数据，还会向后读一个，返回-1；
            System.out.println("-------bufferedInputStreamTest-------");
            while ((len=bi.read(b))!=-1){
                System.out.println(new String(b,0,len));
                //String(byte bytes[], int offset, int length),参数1是缓冲数据的数组，参数2是从数组的那个位置开始转化字符串，参数3是
                //总共转化几个字节；
            }
            bi.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void bufferedOutputStreamTest(String path,String txt){
        try {
            FileOutputStream out=new FileOutputStream(path);

            BufferedOutputStream bo=new BufferedOutputStream(out);


            String s=txt;
            bo.write(s.getBytes());//void write(byte b[])
            bo.flush();
            bo.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void bufferedReaderTest(String path){
        try {
            FileReader fileReader=new FileReader(path);
            BufferedReader br=new BufferedReader(fileReader);
            char[] c=new char[10];
            int len=0;
            System.out.println("-------fileReaderTest-------");
            while ((len=br.read(c))!=-1){
                System.out.println(new String(c,0,len));
            }
            br.close();
            fileReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void bufferedWriterTest(String path,String txt){
        try {
            FileWriter fileWriter=new FileWriter(path);
            BufferedWriter bw=new BufferedWriter(fileWriter);
            bw.write(txt);
            bw.flush();
            bw.close();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 缓冲字符流复制文件
     */
    public static void bufferedCopyFile(String sourcePath,String targetPath){
        try {
            BufferedReader br=new BufferedReader(new FileReader(sourcePath));
            BufferedWriter bw=new BufferedWriter(new FileWriter(targetPath));
            char[] b=new char[1024];
            int len=0;
            while ((len=br.read(b))!=-1){
                bw.write(b,0,len);
            }
            bw.flush();
            bw.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
